<template>
  <div class="login">
    <el-card class="login-card" shadow="always">
      <div class="login-header">
        <h1>企业大宗商品供应链优化系统</h1>
        <p class="subtitle">Enterprise Commodity Supply Chain System</p>
      </div>
      <el-form
        ref="loginForm"
        :model="loginUser"
        :rules="rules"
        size="large"
        label-position="top"
        style="width: 360px; margin: 0 auto"
      >
        <el-form-item label="账号" prop="userCode">
          <el-input 
            v-model="loginUser.userCode"
            prefix-icon="User"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="userPwd">
          <el-input 
            type="password" 
            v-model="loginUser.userPwd" 
            show-password
            prefix-icon="Lock"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="verificationCode">
          <div class="verification-code-container">
            <el-input 
              v-model="loginUser.verificationCode"
              placeholder="请输入验证码"
            >
            </el-input>
            <el-image
              class="verification-code-image"
              :src="codeSrc"
              @click="codeSrc=codeSrc+'?'+new Date()"
            ></el-image>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="login"
            type="primary"
            size="large"
            class="login-button"
          >登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { post, tip, setLocalToken, WAREHOUSE_CONTEXT_PATH } from "@/common";

const loginForm = ref();

const router = useRouter(); // 获取路由器

const loginUser = reactive({ 
  userCode: "admin", 
  userPwd: "123456",
  verificationCode: ""
});

// 验证码后台接口
const codeSrc = ref(WAREHOUSE_CONTEXT_PATH + "/captcha/captchaImage");

const rules = reactive({
  userCode: [
    {
      required: true,
      message: "请输入账号！",
      trigger: "blur"
    },
  ],
  userPwd: [
    {
      required: true,
      message: "请输入密码！",
      trigger: "blur"
    },
  ],
  verificationCode: [
    {
      min: 4,
      max: 4,
      required: true,
      message: "请输入正确的验证码！",
      trigger: "blur"
    }
  ]
});

// 回车登录
window.onkeydown = ($event) => {
  if($event.key && $event.key == 'Enter'){
    login();
  }
}

const login = () => {
  loginForm.value.validate((valid) => {
    if (valid) {
      // 向服务端发出post请求
      post("/login", loginUser).then((result) => {
        // 从回送结果中取出令牌，并将令牌存于客户端
        setLocalToken(result.data);
        tip.success("登录成功！");
        // 通过路由导航到home页面
        router.push({ path: "/home" });
      });
    }
  });
};
</script>

<style scoped>
.login {
  background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("/login.jpg");
  background-size: cover;
  background-position: center;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-card {
  width: 480px;
  padding: 2rem;
  border-radius: 15px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1) !important;
  backdrop-filter: blur(10px);
  background-color: rgba(255, 255, 255, 0.95) !important;
}

.login-header {
  text-align: center;
  margin-bottom: 2rem;
}

.login-header h1 {
  color: #409eff;
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 0.5rem;
  letter-spacing: 2px;
}

.subtitle {
  color: #909399;
  font-size: 14px;
  margin-top: 0.5rem;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #606266;
}

:deep(.el-input__wrapper) {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  border-radius: 8px;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.1);
}

.verification-code-container {
  display: flex;
  gap: 1rem;
  align-items: center;
}

.verification-code-image {
  height: 40px;
  border-radius: 4px;
  cursor: pointer;
  transition: transform 0.2s;
}

.verification-code-image:hover {
  transform: scale(1.02);
}

.login-button {
  width: 100%;
  height: 44px;
  border-radius: 8px;
  font-weight: 500;
  letter-spacing: 1px;
  margin-top: 1rem;
  transition: all 0.3s;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
}
</style>
